package com.chazaqdev.meatbagsorter

class PhotoService {

    static transactional = true
	
	static expose = ["gwt:com.chazaqdev.meatbagsorter.gwt.client"]
	
	String getNextUnsortedPhotoAndLockIt() {
		String[] ret = new String[2]
		ret[0] = "-1"
		ret[1] = "./"
		Photo p = Photo.findByHasBeenProcessedAndPhotoIsLocked(false, false)
		println("Get next unsorted photo and lock it")
		if (p != null) {
			p.photoIsLocked = true
			ret[0] = "" + p.id
			ret[1] = p.savedToFolder
			p.save()
		}
		
		return ret.encodeAsJSON()
		
	}
	
	String getWARContextPath() {
		Photo p = Photo.findByHasBeenProcessedAndPhotoIsLocked(false, false)
		if (p != null) {
			p.photoIsLocked = true
			return p.encodeAsJSON()
		}
		
	}
	
	String getNext6UnsortedPhotoAndLockIt() {
		String[] values = new String[12]
		int i = 0;
		for (i = 0; i < 12; ++i) {
			values[i] = "-1"
		}
		List photos = Photo.findAllByPhotoIsLockedAndHasBeenProcessed(false, false, [max: 6])
		for (i = 0; i < photos.size(); ++i ) {
			Photo p = photos.get(i)
			values[i] = "" + p.id
			values[i+6] = p.savedToFolder
			p.photoIsLocked = true
		}
		
		String s = values.encodeAsJSON()
		println (s)
		return s
	}
	
	private void setLockOnAPhoto(Photo photo, boolean isLocked) {
		photo.photoIsLocked = isLocked
		photo.save()
	}
	
	void setLockOnAPhoto(long photoID, boolean isLocked) {
		Photo p = Photo.findById(photoID)
		if (p != null) {
			setLockOnAPhoto(p, isLocked)
		}
	}
	
	private void setTagOnPhoto(Photo photo, String tag) {
		String s = tag
		String q
		int iof
		PhotoTag photoTag
		Participant participant
		photo.immediateTag = tag
		while ((iof = s.indexOf (";")) > 0) {
			q = s.substring (0, iof).trim()
			participant = Participant.findByNumber(q)
			if (participant == null) {
				println("Could not find " + q + ", creating")
				participant = new Participant(number : q)
				participant.save()
			}
			s = s.substring (iof+1)
			
			photoTag = new PhotoTag(photo: photo, participant: participant)
			photoTag.save()
		}
	}
	
	void setTagOnPhoto(long photoID, String tag) {
		Photo photo = Photo.findByID(photoID)
		if (photo != null) {
			setTagOnPhoto(photo, tag)
		}
	}
	
	void processAPhoto(long photoID, String tag) {
		println ("Process photo " + photoID)
		Photo photo = Photo.findById(photoID)
		if (photo != null) {
			photo.hasBeenProcessed = true
			setTagOnPhoto photo, tag
			setLockOnAPhoto photo, false
			photo.save()
		} else {
			println("Could not find photo");
		}
	}
	
	int getUnsortedCount() {
		return Photo.countByPhotoIsLockedAndHasBeenProcessed(false, false);
	}
	
	
	String getParticipantsPhotos(String number) {
		String[] results = new String[1]
		results[0] = "-1"
		int i = 0;
		Participant p = Participant.findByNumber(number)
		println("Participant " + number)
		if (p != null) {
			List photos = PhotoTag.findAllByParticipant(p)
			if (photos != null) {
				results = new String[photos.size()*2]
				for (PhotoTag pt in photos) {
					results[i] = pt.photo.id
					++i
					results[i] = pt.photo.savedToFolder
					++i
				}
			}
		} else {
			println("Null!")
		}
		String s = results.encodeAsJSON()
		println(s)
		return s
	}
}
